#!/usr/bin/env groovy

String cron_string = BRANCH_NAME == "master" || BRANCH_NAME == "dev" ? 'H 10,19 * * 1-5' : ''

pipeline {
    options {
        overrideIndexTriggers(false)
        buildDiscarder(logRotator(numToKeepStr: '10'))
    }
    agent {label 'aws-1'}
    triggers { 
        cron(cron_string) 
    }
    stages {
        stage('Native check') {
            steps {
               sh "grep -rn 'import com.google.gwt' web-common/src retex/*/src web-dev/src gwtutil/src/ | sed 's/\\([0-9]:\\)/\\1 warning:/' "
               recordIssues tool:java()
            }
        }

        stage('Build Web JS') {
            steps {
                sh './gradlew web:deployIntoWar mergeDeploy -Pgdraft=true -Pgmodule="org.geogebra.web.Web3D,org.geogebra.web.Editor"'
                dir('uitest') {
                    sh 'mkdir content; cp -r ../web/war/* content'
                }
            }
        }

        stage('Build') {
            steps {
                   sh 'docker run --ipc=host --shm-size=1gb -u $(id -u):$(id -g) -e HOME=/work -w /work -v $PWD/uitest:/work -v $PWD/common:/common cypress/browsers:node-16.18.1-chrome-110.0.5481.96-1-ff-109.0-edge-110.0.1587.41-1 sh runtest.sh'
                   lighthouseReport file: './uitest/lighthouse.report.json'
            }
            post {
                always {
                    junit '**/ui-tests.*.xml' 
                    archiveArtifacts artifacts: 'uitest/cypress/videos/**', allowEmptyArchive: true
                }
            }
        }
    }

    post {
        always { cleanAndNotify() }
    }
}
